package cn.edu.hit.database;

import cn.edu.hit.core.db.DataConnection;
import cn.edu.hit.core.db.DataReader;
import cn.edu.hit.kg.Neo4jConfigurationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class FileConnection implements DataConnection {
    static Logger logger = LoggerFactory.getLogger(FileConnection.class);
    private FileDatabase database = null;

    @Override
    public boolean connect(Map connectParams) {
//        System.out.println("In method connect()");
        String path = (String) connectParams.getOrDefault("mongo-data-path", "mongo.data");
        logger.info("测点信息配置文件路径:" + path);
        try {
            InputStream inputStream = Neo4jConfigurationUtil.openFileStream(path);
            database = new FileDatabase(inputStream);
            logger.info("成功读取测点信息配置文件");
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    @Override
    public DataReader getReader() {
        return database;
    }
}
